package com.qire.manhua.model.viewModel.impl;

import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.method.MovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;

import com.qire.antsrouter.AntsRouter;
import com.qire.common.constant.Navigation;
import com.qire.common.model.entity.UserEntity;
import com.qire.manhua.BR;

import androidx.annotation.NonNull;
import androidx.databinding.BaseObservable;
import androidx.databinding.Bindable;

/**
 * 漫画下载解锁弹窗ViewModel
 */
public class ComicDownloadUnlockViewModel extends BaseObservable {

    private int   downloadCount = 0, unlockCount = 0;
    private float buyPrice      = 0;
    private float vipBuyPrice   = 0;
    private float originalPrice = 0;
    private float discount      = 0;

    public ComicDownloadUnlockViewModel() {}

    public float originalPrice() {
        return originalPrice;
    }

    public void setCountInfo(int downloadCount, int unlockCount) {
        this.downloadCount  = downloadCount;
        this.unlockCount    = unlockCount;
        notifyPropertyChanged(BR.introduction);
    }

    public void setBuyPrice(float buyPrice) {
        this.buyPrice = buyPrice;
        notifyPropertyChanged(BR.buyChapterCoinPrompt);
        notifyPropertyChanged(BR.paymentBtnText);
    }

    public void setDiscountInfo(float originalPrice, float vipBuyPrice, float discount) {
        this.originalPrice  = originalPrice;
        this.vipBuyPrice    = vipBuyPrice;
        this.discount       = discount;
        notifyPropertyChanged(BR.discountInfo);
        notifyPropertyChanged(BR.discountInfoMovementMethod);
    }

    public boolean isInsufficientBalance() {
        float coinBalance = UserEntity.self.getCoinBalance();
        return this.buyPrice > coinBalance;
    }


    @Bindable
    public CharSequence getIntroduction() {
        return "您本次下载共" + downloadCount + "话，其中需要解锁" + unlockCount + "话";
    }
    @Bindable
    public MovementMethod getDiscountInfoMovementMethod() {
        if(UserEntity.self.isVip()) {
            return null;
        }
        return LinkMovementMethod.getInstance();
    }
    @Bindable
    public CharSequence getDiscountInfo() {
        SpannableStringBuilder builder = new SpannableStringBuilder();
        if (UserEntity.self.isVip()) {
            builder.append("原价" + originalPrice + "金币，")
                    .append("正在享受").append(UserEntity.self.getVipType().name);
            int startIndex = builder.length();
            builder.append((int) discount + "折");
            int endIndex = builder.length();
            builder.append("优惠");

            ForegroundColorSpan fgSpan = new ForegroundColorSpan(Color.rgb(0xff, 0x54, 0x20));
            builder.setSpan(fgSpan, startIndex, endIndex, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        } else {
            builder.append("会员解锁只需")
                    .append(vipBuyPrice + "金币，");
            int startIndex = builder.length();
            builder.append("开通会员");

            ForegroundColorSpan fgSpan = new ForegroundColorSpan(Color.rgb(0x48, 0x8a, 0xe5));
            ClickableSpan clickableSpan = new ClickableSpan() {
                @Override
                public void onClick(@NonNull View widget) {
                    AntsRouter.ROUTER.buildCard(Navigation.Page.BuyMembershipPage).navigation();
                }

                @Override
                public void updateDrawState(@NonNull TextPaint ds) {
                }
            };
            builder.setSpan(clickableSpan, startIndex, builder.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
            builder.setSpan(fgSpan, startIndex, builder.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        }

        return builder;
    }
    @Bindable
    public CharSequence getBuyChapterCoinPrompt() {
        SpannableStringBuilder builder = new SpannableStringBuilder("需支付");

        ForegroundColorSpan prefixFgSpan = new ForegroundColorSpan(Color.rgb(0x33, 0x33, 0x33));
        builder.setSpan(prefixFgSpan, 0, builder.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

        return builder.append(buyPrice + "金币");
    }
    @Bindable
    public String getBalancePrompt() {
        float coinBalance = UserEntity.self.getCoinBalance();
        return "账户余额：" + coinBalance + "金币";
    }
    @Bindable
    public String getPaymentBtnText() {
        return isInsufficientBalance() ? "余额不足，立即充值" : "解锁并缓存";
    }


}
